home *** CD-ROM | disk | FTP | other *** search
/ Beginning Mac Programming / Beginning Mac Programming.bin / Open Me for REALbasic 3 / REALbasic 3.2 / Example Projects / Techniques / User-Defined Binding / Read Me next >
Text File  |  1999-03-17  |  6KB  |  67 lines

  1. Creating User-Defined Binds
  2. By Geoff Perlman
  3. March 17th, 1999
  4.  
  5. Introduction
  6. REALbasic version 2 introduces the concept of binding. Binding allows you to add functionality to your interface without writing any code. There are many binding actions you can choose that are built-in to REALbasic but you can also create your own. One of the neat things about creating your own binding actions is that once created, the user doesn't actually have to look at or understand the code that handles the action. You can drop your custom binding into any project and it will work as transparently just as if it was built-in to REALbasic.
  7.  
  8. Explanation
  9. This first example creates a user defined bind between pushbuttons and listboxes.  It allows the user to create a pushbutton that deletes all rows from a listbox.
  10.  
  11. 1. Add a new class to your project.
  12.  
  13. 2. Name this class "DeleteAllRowsBind".
  14.  
  15. 3. Since this class is going to be a bind, it needs to support the bindingInterface. Also, since you will want to assign an action to the Action event of the pushbutton that when clicked, tells the listbox to delete all the rows. In order to add code that will execute when the user clicks the pushbutton, you will need to support the ActionNotificationReceiver interface. To support the bindingInterface and ActionNotificationReciever interface, enter "bindingInterface,ActionNotificationReceiver" in the Interfaces property of the class.
  16.  
  17. 4. Now that the class supports the bindingInterface, you can add the Bind method. Add a new method to the class and called it "Bind". Include the parameters "Source as Object, Target as Object".
  18.  
  19. 5. Since the code that executes when the user presses the pushbutton will need to know which pushbutton and listbox have been bound, these will need to be stored as properties in the class. Add the following properties to the class: BindSource as Pushbutton button and BindTarget as Listbox.
  20.  
  21. 6. Add the following code to the Bind method:
  22.  
  23. #pragma bindingSpecification pushbutton,listbox,"Delete all rows in %2 when %1 is pushed"
  24. BindSource=pushbutton(source)
  25. bindTarget=listbox(target)
  26.  
  27. BindSource.addActionNotificationReceiver self
  28.  
  29. The #pragma statement defines what will appear in the Bind dialog box when the user binds the two objects by command-shift dragging from the source to the target (the pushbutton to the listbox). It indicates the source class, the target class and the text that will appear in the Bind window. %1 will be the name of the source control and %2 will be the name of the target control.
  30.  
  31. When the bind occurs, the controls that are bound will be passed to the Bind method. However, they are passed as generic objects. In order for the code to store them in the BindSource and BindTarget properties you just added to the class, these two object parameters have to be recast as pushbutton and listbox.  Lines 2 and 3 in the Bind method do just that.
  32.  
  33. finally, the last line connects the bind class to the Action event of the pushbutton by calling the addActionNotificationReceiver method of the pushbutton class. Self is passed as a parameter to this method and represents the class itself.
  34.  
  35. 7. Now you need to add the code that will delete the rows when the user clicks the pushbutton. To do this, add a method called "PerformAction" to the class.
  36.  
  37. 8. The code for the PerformAction method is simple:
  38.  
  39. bindTarget.DeleteAllRows
  40.  
  41. 9. To try out this new bind, drag a listbox and a pushbutton onto a window, add some rows of data to the listbox then link the pushbutton to the listbox by command-shift dragging from the pushbutton to the listbox. When the Bind dialog appears, choose the option "Delete all rows from listbox1 when pushbutton1 is pushed" then run the project and click the button.
  42.  
  43. Creating a Bind that deletes only the selected row
  44. 1. To create a bind that will delete the selected row from a listbox when a pushbutton is pushed,  create another class in your project and duplicate all the steps you did above.
  45.  
  46. 2. Since this version of the bind should only work when a row is selected, you will need to support a few more interfaces in order to detect when the user selects a row. Add listSelectionNotificationReceiver to the list of interfaces in the Interfaces property of the class.
  47.  
  48. 3. Change the last parameter of the #pragma statement in the Bind method to "Delete the selected row in %2 when %1 is pushed".
  49.  
  50. 4. Add the the following two lines to the end of the Bind method:
  51.  
  52. BindTarget.addListSelectionNotificationReceiver self
  53. bindSource.enabled=(bindTarget.listindex >= 0)
  54.  
  55. 5. The first line links the bind class to any change to the list selection. The second line, sets the source (the pushbutton) to enabled if a row of the target (the listbox) is selected and disables the source if no row is selected when the window opens.
  56.  
  57. 6. Now you need to add the code that will disable or enable the pushbutton once the window is open the user begins selecting or deselecting rows in the listbox. In order to do this, you will need to add the methods that are part of the ListSelectionNotificationReceiver interface to the class. Add methods "SelectionChanged" and "SelectionChanging" to the class. Niether has any parameters. Add the following code to the SelectionChanged method:
  58.  
  59. bindSource.enabled=(bindTarget.listindex >= 0)
  60.  
  61. Anytime the selection in the listbox is changed, the bind will be notified and the SelectionChanged method will fire.
  62.  
  63. 7. Lastly, you need to add the code that will delete the selected row when the user clicks the pushbutton. Add the following code to the PerformAction method:
  64.  
  65. bindTarget.removeRow bindTarget.listindex
  66.  
  67. 8. Now you can add another button and bind it to the listbox choosing your new bind as the bind action.